//
//  ChangePasswordViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/8.
//

#import "ChangePasswordViewController.h"
#import <SangforSDK/SFUemSDK.h>


@interface ChangePasswordViewController ()

@property (nonatomic,strong) UITextField * oldPwdField;
@property (nonatomic,strong) UITextField * NewPwdField;
@property (nonatomic,strong) UITextField * confirmPwdField;
//@property (nonatomic,strong) UITextField * codeField;
//@property (nonatomic,strong) UIButton * codeBtn ;


@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改密码";
    
    // 旧密码
    UILabel *oldPwdLabel = [[UILabel alloc] init];
    oldPwdLabel.text = @"请输入旧密码";
    oldPwdLabel.font = FONT14;
    oldPwdLabel.textColor = [UIColor blackColor];
    [self.view addSubview:oldPwdLabel];
    [oldPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.view).offset(StatusBarHeight + ALIGN_HEIGHT_PT(20));
    }];
    
    self.oldPwdField = [[UITextField alloc] init];
    self.oldPwdField.placeholder = @"请输入旧密码";
    self.oldPwdField.secureTextEntry = YES;
    self.oldPwdField.font = FONT14;
    [self.view addSubview:self.oldPwdField];
    [self.oldPwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oldPwdLabel);
        make.right.equalTo(self.view).offset(-ALIGN_WIDTH_PT(15));
        make.top.equalTo(oldPwdLabel.mas_bottom).offset(ALIGN_HEIGHT_PT(10));
        make.height.mas_equalTo(ALIGN_HEIGHT_PT(44));
    }];
    
    
    // 新密码
    UILabel *newPwdLabel = [[UILabel alloc] init];
    newPwdLabel.text = @"请输入新密码";
    newPwdLabel.font = FONT14;
    newPwdLabel.textColor = [UIColor blackColor];
    [self.view addSubview:newPwdLabel];
    [newPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oldPwdLabel);
        make.top.equalTo(self.oldPwdField.mas_bottom).offset(ALIGN_HEIGHT_PT(20));
    }];
    
    self.NewPwdField = [[UITextField alloc] init];
    self.NewPwdField.placeholder = @"密码必须包含数字、字母、特殊符号，至少8位";
    self.NewPwdField.secureTextEntry = YES;
    self.NewPwdField.font = FONT14;
    [self.view addSubview:self.NewPwdField];
    [self.NewPwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.oldPwdField);
        make.top.equalTo(newPwdLabel.mas_bottom).offset(ALIGN_HEIGHT_PT(10));
    }];
    
    // 确认密码
    UILabel *confirmPwdLabel = [[UILabel alloc] init];
    confirmPwdLabel.text = @"请确认密码";
    confirmPwdLabel.font = FONT14;
    confirmPwdLabel.textColor = [UIColor blackColor];
    [self.view addSubview:confirmPwdLabel];
    [confirmPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oldPwdLabel);
        make.top.equalTo(self.NewPwdField.mas_bottom).offset(ALIGN_HEIGHT_PT(20));
    }];
    
    self.confirmPwdField = [[UITextField alloc] init];
    self.confirmPwdField.placeholder = @"密码必须包含数字、字母、特殊符号，至少8位";
    self.confirmPwdField.secureTextEntry = YES;
    self.confirmPwdField.font = FONT14;
    [self.view addSubview:self.confirmPwdField];
    [self.confirmPwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.oldPwdField);
        make.top.equalTo(confirmPwdLabel.mas_bottom).offset(ALIGN_HEIGHT_PT(10));
    }];
    
    // 验证码
//    UILabel *codeLabel = [[UILabel alloc] init];
//    codeLabel.text = @"短信验证码";
//    codeLabel.font = FONT14;
//    codeLabel.textColor = [UIColor blackColor];
//    [self.view addSubview:codeLabel];
//    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(oldPwdLabel);
//        make.top.equalTo(self.confirmPwdField.mas_bottom).offset(ALIGN_HEIGHT_PT(20));
//    }];
    
//    self.codeField = [[UITextField alloc] init];
//    self.codeField.placeholder = @"请输入验证码";
//    self.codeField.font = FONT14;
//    [self.view addSubview:self.codeField];
//    
//    self.codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
//    [self.codeBtn setTitleColor:COLOR_RGB(50, 113, 237) forState:UIControlStateNormal];
//    self.codeBtn.titleLabel.font = FONT14;
//    [self.codeBtn addTarget:self action:@selector(respondsTo_checkCodeBtn) forControlEvents:(UIControlEventTouchUpInside)];
//    [self.view addSubview:self.codeBtn];
    
//    [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.oldPwdField);
//        make.top.equalTo(codeLabel.mas_bottom).offset(ALIGN_HEIGHT_PT(10));
//        make.height.equalTo(self.oldPwdField);
//        make.right.equalTo(self.codeBtn.mas_left).offset(-ALIGN_WIDTH_PT(10));
//    }];
//    
//    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.codeField);
//        make.right.equalTo(self.oldPwdField);
//        make.width.mas_equalTo(ALIGN_WIDTH_PT(100));
//        make.height.equalTo(self.codeField);
//    }];
    
    // 确认按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = FONT16;
    confirmBtn.backgroundColor = COLOR_RGB(50, 113, 237);
    confirmBtn.layer.cornerRadius = 5;
    [confirmBtn addTarget:self action:@selector(respondsto_configmBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:confirmBtn];

    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.oldPwdField);
        make.bottom.equalTo(self.view).offset(-ALIGN_HEIGHT_PT(40));
        make.height.mas_equalTo(ALIGN_HEIGHT_PT(44));
    }];
//    self.oldPwdField.text =   @"Epss@123456";
//    self.NewPwdField.text =   @"Epss@1234567";
//    self.confirmPwdField.text =   @"Epss@1234567";
//    [[SFUemSDK sharedInstance].auth /*sms*/
    
    [self doResetPwd];
}



- (void)doResetPwd
{
    if ([[SFUemSDK sharedInstance].auth allowResetPassword]) {
        __block NSString *pwdRule = @"密码规则:";
        
        [[SFUemSDK sharedInstance].auth getPswStrategy:^(NSString * _Nullable displayMessage, NSString * _Nullable detailRuleJson) {
            NSLog(@"TestListViewController displayMessage is:%@, detailRuleJson:%@", displayMessage, detailRuleJson);
            pwdRule = [NSString stringWithFormat:@"%@%@", pwdRule, displayMessage];
//            [self showRenewPasswordAuthAlert:pwdRule];
        }];
    } else {
//        [self showAlertView:@"当前不支持主动修改密码" message:@""];
    }
}


#pragma mark -- 点击事件
//-(void)respondsTo_checkCodeBtn{
//    NSString * uemPhone = [NSString stringWithFormat:@"86-%@@sms",self.phoneText];
//    [[SFUemSDK sharedInstance].auth startPrimarySmsAuth:[NSURL URLWithString:@"https://kzqtxzvpn.caacsri.com:9998"] phoneNumber:uemPhone];
////    86-185xxxxxx8626@sms
//    // 禁用按钮
//    self.codeBtn.enabled = NO;
//    
//    // 开始60s倒计时
//    __block int timeout = 60;
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//    dispatch_source_set_event_handler(timer, ^{
//        if(timeout <= 0){
//            dispatch_source_cancel(timer);
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
//                self.codeBtn.enabled = YES;
//            });
//        } else {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.codeBtn setTitle:[NSString stringWithFormat:@"(%ds)后重试",timeout] forState:UIControlStateNormal];
//            });
//            timeout--;
//        }
//    });
//    dispatch_resume(timer);
//}





-(void)respondsto_configmBtn{
    
//    [SFUemSDK sharedInstance].auth
    
    // 判断密码是否符合要求
    NSString *password = self.NewPwdField.text;
    
    // 密码规则:至少8位,必须包含数字、字母、特殊符号
    NSString *regex = @"^(?=.*[0-9])(?=.*[A-Za-z])(?=.*[^A-Za-z0-9]).{8,}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:password];
    
    if (!isValid) {
        NSMutableString *errorMsg = [NSMutableString stringWithString:@"密码格式不正确:\n"];
        if (password.length < 8) {
            [errorMsg appendString:@"• 密码长度至少8位\n"];
        }
        if (![password containsString:@"[0-9]"]) {
            [errorMsg appendString:@"• 必须包含数字\n"];
        }
        if (![password containsString:@"[A-Za-z]"]) {
            [errorMsg appendString:@"• 必须包含字母\n"];
        }
        if (![password containsString:@"[^A-Za-z0-9]"]) {
            [errorMsg appendString:@"• 必须包含特殊符号"];
        }
        [MBProgressHUD showError:errorMsg];
        return;
    }
    [[SFUemSDK sharedInstance] doSecondaryAuth:SFAuthTypeRenewPassword data:@{kAuthKeyRenewOldPassword:self.oldPwdField.text,
                kAuthKeyRenewNewPassword:self.NewPwdField.text,                                                                            }];//
//    [[SFUemSDK sharedInstance].auth resetPassword:self.oldPwdField.text newPassword:self.NewPwdField.text handler:^(NSError * _Nullable error) {
//        if (!error) {
//            [self.navigationController popViewControllerAnimated:true];
//        } else {
//            [LUtilityTools showAlertWithTitle:@"温馨提示" message:error.domain buttonAction:^{
//                
//            }];
//        }
//    }];
    
    
    // 密码验证通过,继续后续操作...
    
}



@end
